package nina.yiyou.attendance.s10merge.output

import groovy.transform.PackageScope
import nina.yiyou.salary.output.util.CsvLineMaker
import nina.yiyou.salary.output.util.OutCsvWriter

import java.nio.file.Path

@PackageScope
class ResultCsvExporter {

  ResultCsvExporter(Path csvPath, List<Map> dayList, List<String> keyList) {
    _csvPath = csvPath
    _dayList = dayList
    _keyList = keyList
  }

  void export() {
    List header = [new CsvLineMaker(_keyList).make()]
    new OutCsvWriter(_csvPath, header + _dayList.collect { toCsvLine(it) }).write()
  }

  private String toCsvLine(Map lineMap) {
    List lineVal = _keyList.collect { lineMap.containsKey(it) ? lineMap[it] : '' }
    return new CsvLineMaker(lineVal).make()
  }

  private final Path _csvPath

  private final List<String> _keyList
  private final List<Map> _dayList
}
